package com.bolt.auth.security.admin.biz.sys.controller;


import com.bolt.auth.admin.api.sys.dto.MenuDTO;
import com.bolt.auth.admin.api.sys.dto.MenuQueryCriteria;
import com.bolt.auth.admin.api.sys.dto.converter.MenuConverter;
import com.bolt.auth.admin.api.sys.entity.MenuEntity;
import com.bolt.auth.admin.api.sys.vo.MenuVO;
import com.bolt.auth.security.admin.biz.sys.service.MenuService;
import com.bolt.auth.security.common.model.AuthUserDetails;
import com.bolt.auth.security.common.utils.SecurityUtils;
import com.bolt.common.collection.CollectionUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.ui.tree.helper.TreeRenderHelper;
import com.bolt.support.ui.tree.vo.UITreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：菜单管理")
@RequestMapping("/api/menus")
public class MenuController {

    private final MenuService menuService;


    @PostMapping
    @ApiOperation("菜单新增")
    public ResultMessage create(@Validated @RequestBody MenuDTO resources) {
        menuService.saveOrUpdate(resources);
        return Results.success();
    }

    @PutMapping
    @ApiOperation(notes = "菜单编辑", httpMethod = "PUT", value = "菜单编辑")
    public ResultMessage<Void> update(@Validated(MenuDTO.Update.class) @RequestBody MenuDTO dao) {
        menuService.saveOrUpdate(dao);
        return Results.success();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping
    public ResultMessage delete(@RequestBody Set <Long> ids) {
        List <MenuEntity> allMenus = new ArrayList <>();
        for (Long id : ids) {
            List <MenuEntity> menuList = menuService.getMenus(id);
            allMenus.add(menuService.findOne(id).orElse(new MenuEntity()));
            allMenus = menuService.findAllChildren(menuList, allMenus);
        }
        menuService.delete(allMenus);
        return Results.success();
    }

    @GetMapping
    @ApiOperation("查询菜单")
    public ResultMessage page(MenuQueryCriteria queryCriteria) {
        if (!queryCriteria.isQuery()) {
            queryCriteria.setPidIsNull(true);
        }
        List <MenuEntity> data = menuService.queryMenus(queryCriteria);
        return Results.success(MenuConverter.toDTO(data));
    }

    @GetMapping(value = "getTree")
    public List <UITreeNode> getTree(@RequestParam(name = "pid", required = false) Long id, @RequestParam Map <String, String> params) {
        boolean loadAllNodes = StrUtil.isEmpty(params.get("loadAllNodes")) ? false : Boolean.parseBoolean(params.get("loadAllNodes"));
        List <UITreeNode> dataArray = new ArrayList <>();
        TreeRenderHelper.buildNodesTreeView(menuService, id, params, dataArray, loadAllNodes);
        return dataArray;
    }

    @GetMapping(value = "/build")
    @ApiOperation("获取前端所需菜单")
    public ResultMessage<List <MenuVO>> buildMenus() {
        AuthUserDetails userDetails =  SecurityUtils.getUser();
        List <MenuDTO> menuDtos;
        if (userDetails.isSuperUser()) {
            menuDtos = menuService.findAllMenus();
        } else {
            menuDtos = menuService.findByUser(userDetails.getUserId());
        }
        List <MenuDTO> treeMenus = menuService.buildTreeDto(menuDtos);
        return Results.success(menuService.buildMenus(treeMenus));
    }

    @ApiOperation("查询菜单:根据ID获取同级与上级数据")
    @PostMapping("/findAll")
    public ResultMessage<List <UITreeNode>> findAll(@RequestBody List <Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            List <MenuEntity> menuEntities = menuService.findAll(ids);
            Results.success(menuService.buildTree(menuEntities));
        }
        return Results.success(menuService.buildTree(menuService.findRoot()));
    }

    /**
     * 根据菜单生成生权限资 源
     *
     * @return
     */
    @PostMapping("/importPrivilege")
    public ResultMessage importPrivilege(Long[] menuIds) {
        menuService.impMenu2Privilege(menuIds, true);
        return Results.success();
    }
}

 